package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.feign.item.SkuDetailFeignClient;
import com.atguigu.gmall.model.to.SkuDetailTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@Controller
public class ItemController {

    @Autowired
    private SkuDetailFeignClient skuDetailFeignClient;

    @GetMapping("/{skuId}.html")
    public String indexPage(@PathVariable("skuId")Long skuId,Model model){
        Result<SkuDetailTo> result = skuDetailFeignClient.getSkuDetail(skuId);

        if (result.isOk()) {
            SkuDetailTo skuDetailTo = result.getData();

            if (skuDetailTo.getSkuInfo() == null || skuDetailTo == null){
                return "/item/404";
            }

            model.addAttribute("categoryView",skuDetailTo.getCategoryView());
            model.addAttribute("skuInfo",skuDetailTo.getSkuInfo());
            model.addAttribute("price",skuDetailTo.getPrice());
            model.addAttribute("spuSaleAttrList",skuDetailTo.getSpuSaleAttrList());
            model.addAttribute("valuesSkuJson",skuDetailTo.getValuesSkuJson());
        }
        return "item/index";
    }
}
